#include "mystring.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
#include <stdlib.h>
int Init_string(mystring *obj,const char *str);
void Print(mystring *obj);
int Size1(mystring *obj);
int Isequal(mystring *obj1,mystring *obj2);
int Iscontains(mystring *obj1,mystring *obj2);
char* Atindex(mystring *obj,int index);
void Removestring(mystring *obj,const char *p);
int Insertstring(mystring *obj1,const char *p,int index);
int Toint(mystring *obj);
double Todouble (mystring *obj);
void Freestring(mystring *obj);
int Reallocarray(mystring  *obj);


int Init_string(mystring *obj, const char *str)
{
    obj->size=strlen(str);
    obj->cubage=obj->size+1;
    obj->string=(char*)malloc(obj->size+1);
    if(obj->string==NULL)
    {
        printf("maalloc error\n");
    }
    strcpy(obj->string,str);
    
    obj->print=Print;
    obj->isequal=Isequal;
    obj->size1=Size1;
    obj->atindex=Atindex;
    obj->insertstring=Insertstring;
    obj->removestring=Removestring;
    obj->iscontains=Iscontains;
    obj->toint=Toint;
    obj->todouble=Todouble;
    obj->freestring=Freestring;
    obj->reallocarray=Reallocarray;
}

void Print(mystring *obj)
{
    printf("str cubage:%d  str size:%d\n",obj->cubage,obj->size);
    printf("----------------------------------\n");
    printf("%s\n",obj->string);
    printf("----------------------------------\n");
}

int Size1(mystring *obj)
{
    return obj->size;
}

int Isequal(mystring *obj1, mystring *obj2)
{
    if(strcmp(obj1->string,obj2->string)==0)
        return 1;
    return 0;
}

int Iscontains(mystring *obj1, mystring *obj2)
{
    char *equal=strstr(obj1->string,obj2->string);
    if(equal==NULL)
        return 0;
    else
        return 1;
}

char* Atindex(mystring *obj, int index)
{
    if(index>=obj->size||index<0)
        return NULL;
    char *p=obj->string;
    while(index!=0)
    {
        p++;
        index--;
    }
    return p;
}

void Removestring(mystring *obj, const char *p)
{
    char *ptr=strstr(obj->string,p);
    if(ptr==NULL)
        return;
    else
    {
        char *back=ptr+strlen(p);
        while(*back!='\0')
        {
            *ptr++=*back++;
        }
        *ptr='\0';
        obj->size=strlen(obj->string);
    }
}

int  Insertstring(mystring *obj1,const char *p, int index)
{
    if(index<0||index>obj1->size)
    {
        printf("invalid place\n");
        return false;
    }
    int new_need_len=obj1->size+strlen(p)+1;
    char *new_str=(char*)malloc(sizeof(char)*new_need_len);
    if(new_str==NULL)
    {
        printf("can not contain more char\n");
        return false;         

    }
    strncpy(new_str,obj1->string,index);
    strncpy(new_str+index,p,strlen(p));
    strcpy(new_str+index+strlen(p),obj1->string+index);
    free(obj1->string);
    obj1->string=new_str;
    obj1->size=strlen(obj1->string);
    obj1->cubage=obj1->size+1;
    return true;
}

int Toint(mystring *obj)
{
    char *p=obj->string;
    int sign=1;
    int result=0;
    if(*p=='-')
    {
        sign=-1;
        p++;
    }
    while(*p!='\0')
    {
        if(*p>='0'&&*p<='9')
        {       
            result=result*10+*p-'0';
        }
        p++;
        if(*p=='.')
        return result*sign;
    }
    return result*sign;
}

double Todouble(mystring *obj)
{
    char *p=obj->string;
    int result =0;
    int sign=1;
    int count=0;
    int n=0;
    while(*p!='.'&&*p!='\0')
    {
        if(*p=='-')
        {
            sign =-1;
        }
        if(*p<='9'&&*p>='0')
        {
            result =result *10+*p-'0';
        }
        p++;
    }
    p++;
    while(*p !='\0')
    {
        if(*p<='9'&&*p>='0')
        {
            count =count *10+*p-'0';
            n++;
        }
        p++;   
    }
    return sign*((double)result+(double)count/pow(10,n));
}

void Freestring(mystring *obj)
{
    if(obj->string!=NULL)
    {
        free(obj->string);
        obj->string=NULL;
        obj->size=0;
        obj->cubage=0;
    }
}

int Reallocarray(mystring  *obj)
{
    char *temp=obj->string;
    obj->cubage=obj->cubage*2;
    obj->string=(char *)malloc(sizeof(char)*obj->cubage);
    if(obj->string==NULL)
    {
        printf("realloc error!\n");
        obj->string=temp;
        obj->cubage=obj->cubage/2;
        return false;
    }
    strcpy(obj->string,temp);
    
    free(temp);
    temp=NULL;
    return true;

}
